﻿using Lion.WeiXin.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lion.WeiXin.Core.Data;
using System.Reflection;
namespace Lion.WeiXin.Core
{
    /// <summary>
    /// 自定义微信任务,反射AutoReply 表中的DoTask 方法
    /// </summary>
    public class CustomTask
    {
        public static readonly CustomTask Instance = new CustomTask();

        DBContext db = new DBContext();

        /// <summary>
        /// 执行任务方法
        /// </summary>
        /// <param name="funName"></param>
        /// <param name="funParams"></param>
        public static object Execute(string funName, params object[] funParams)
        {
            Type t = Type.GetType("Lion.WeiXin.Core.CustomTask");
            CustomTask obj = (CustomTask)Activator.CreateInstance(t);
            //获取方法的信息
            MethodInfo method = t.GetMethod(funName);
            //调用方法的一些标志位，这里的含义是Public并且是实例方法，这也是默认的值
           // BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
            //GetValue方法的参数
            object[] parameters = funParams;
            //调用方法，用一个object接收返回值
           // object returnValue = method.Invoke(obj, flag, Type.DefaultBinder, parameters, null);
            object returnValue = method.Invoke(obj, parameters);
            return returnValue;
        }

        /// <summary>
        /// 文本关键字：查询余额
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public string GetBalance(string openId)
        {
            string result = string.Empty;
            WeChatUserModel wcUser = db.WeChatUsers.GetModel(openId);
            if (wcUser != null)
            {

                VipCardModel vipCard = db.VipCards.GetModel(wcUser.VipCardCode);
                if (vipCard != null)
                {
                    result = string.Format("尊敬的{0},您的卡号余额是：￥{1}", wcUser.NickName, vipCard.Balance);
                }
                else
                {
                    result = "很抱歉，您还没办理会员卡！";
                }
            }
            else
            {
                result = "请您先关注我们的公众号！";
            }
            return result;
        }

        /// <summary>
        /// 任意指令方法：确认客户支付验证码,有验证码，就处理，无验证码回复空
        /// </summary>
        /// <param name="openId">微信openid</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public string ConfirmPaymentCode(string openId,string code)
        {
            Lion.WeiXin.Tools.Log.LogHelper.Info("ConfirmPaymentCode:openid={0},code={1}", openId, code);
            string where =string.Format( "OpenId='{0}' and Code='{1}'",openId,code);
            ValidateModel model = db.Validates.GetList(where).FirstOrDefault();
            string result = string.Empty;
            if (model != null)
            { 
                //验证码是否过期
                if (DateTime.Now >= model.StartDate && DateTime.Now <= model.EndDate)
                {
                   OrderModel odr =  db.Orders.GetModel(model.BizId);
                   odr.Payment = odr.Total;
                   odr.Status = (int)OrderStateEnum.结单;
                   odr.LastUpdateDate = DateTime.Now;
                   db.Orders.PayBill(odr);
                   //记录日志
                   VipCardUseLogHelper.WriteLog(odr.Buyer, "支付订单", VipCardUseEnum.消费, odr.Code, odr.Payment);

                   //删除验证码
                   db.Validates.Delete(model.AID);
                   result = "验证通过，订单支付成功！";
                }
                else
                {
                    result = "验证码已过期，请重新获取验证码！";
                }
            }
            return result;
        }
    }
}
